<?php
include_once(APPLIBS . 'simple_html_dom.php');
App::uses('HttpSocket', 'Network/http');
App::uses('Wordpress', 'Model');

class CrawlerShell extends AppShell {
	public function main() {
		$Wordpress = new Wordpress();
		$Wordpress->updateAll(array('del_flg' => 1));
		
		$mapItem = $Wordpress->find('list', array('fields' => array('link', 'id')));
		
		$html = file_get_html(SPIDER_WORDPRESS);

		$themeLink = array();
		
		foreach($html->find('.item-info h3 a') as $element) {
			$href = $element->href;
			array_push($themeLink, array('link' => $href));
		}
		
// 		$pageNumber = array();
// 		foreach($html->find('a[class="page_number"]') as $element) {
// 			$href = $element->href;
// 			preg_match('/\/category\/wordpress\?page=([\d]+)/', $href, $matches);
// 			array_push($pageNumber, $matches[1]);
// 		}
		
// 		$max = max($pageNumber);
		
// 		for ($i = 2; $i <= $max; $i++) {
// 			$this->out($i);
// 			$html = file_get_html(SPIDER_WORDPRESS . '?page=' . $i);
			
// 			foreach($html->find('.item-info h3 a') as $element) {
// 				$href = $element->href;
// 				array_push($themeLink, array('link' => $href));
// 			}
// 		}
		
		$length = count($themeLink);
		for ($i = 0; $i < $length; $i++) {
			$itemInfo = $this->__getItemInfo($themeLink[$i]['link']);
			$themeLink[$i] = $itemInfo;
			if (isset($mapItem[$themeLink[$i]['link']])) {
				$themeLink[$i]['id'] = $mapItem[$themeLink[$i]['link']];
				$themeLink[$i]['del_flg'] = 0;
			}
		}
		
		$Wordpress->saveMany($themeLink);
	}
	
	private function __getItemInfo($url) {
		// Array which content all information of item
		$itemTemplate = array(
			'link' => $url,
			'author' => null,
			'item' => null,
			'category' => null,
			'regular_price' => null,
			'extended_price' => null,
			'rating_value' => null,
			'rating_count' => null,
			'purchases' => null,
			'comments' => null,
			'profile' => null,
			'date_created' => null,
			'date_last_modified' => null,
			'date_last_system_updated' => date('Y-m-d')
			
		);
		
		// Item's HTML contents
		$html = file_get_html($url);
		
		// Get basic infomation
		$parseUrl = parse_url($url);
		$finder = explode('/', $parseUrl['path']);
		
		if (isset($finder[3])) {
			$itemTemplate['forest_id'] = $finder[3];
		}
		
		if (isset($finder[2])) {
			$itemTemplate['item'] = $finder[2];
		}
		
		parse_str($parseUrl['query'], $finder);
		if (isset($finder['WT_z_author'])) {
			$itemTemplate['author'] = $finder['WT_z_author'];
		} else {
			$finder = $html->find('.decorator a', 0);
			if ($finder) {
				$itemTemplate['author'] = trim($finder->plaintext);
			}
		}
		
		// Get Profile
		$profileAttrName = array();
		foreach ($html->find('#item_attributes', 0)->find('.attr-name') as $attrName) {
			array_push($profileAttrName, trim($attrName->plaintext));
		}
		$profileAttrDetail = array();
		foreach ($html->find('#item_attributes', 0)->find('.attr-detail') as $attrDetail) {
			array_push($profileAttrDetail, trim($attrDetail->plaintext));
		}
		$profile = array_combine($profileAttrName, $profileAttrDetail);
		$itemTemplate['profile'] = serialize($profile);
		$itemTemplate['date_created'] = date('Y-m-d', strtotime($profile['Created']));
		$itemTemplate['date_last_modified'] = date('Y-m-d', strtotime($profile['Last Update']));

		// Get category
		$finder = $html->find('#breadcrumbs a');
		if ($finder && is_array($finder)) {
			$finder = end($finder);
			$itemTemplate['category'] = $finder->plaintext;
		}
		
		// Get regular price
		$finder = $html->find('span[data-licence="regular"]', 0);
		if ($finder) {
			$itemTemplate['regular_price'] = trim($finder->plaintext);
		}
		
		// Get extended price
		$finder = $html->find('span[data-licence="extended"]', 0);
		if ($finder) {
			$itemTemplate['extended_price'] = trim($finder->plaintext);
		}
		
		// Get purchases
		$finder = $html->find('.comments-purchases span', 0);
		if ($finder) {
			$itemTemplate['purchases'] = trim($finder->plaintext);
		}
		
		// Get comments
		$finder = $html->find('.comments-purchases span', 1);
		if ($finder) {
			$itemTemplate['comments'] = trim($finder->plaintext);
		}
		
		// Get ratings
		$ratingValue = 0;
		$finder = $html->find('meta[itemprop="ratingValue"]', 0);
		if ($finder) {
			$ratingValue = $finder->content;
		}
		
		$ratingCount = 0;
		$finder = $html->find('meta[itemprop="ratingCount"]', 0);
		if ($finder) {
			$ratingCount = $finder->content;
		}
		
		$this->out('+1');
		
		return $itemTemplate;
	}
}